<template>
  <div>
    <el-dialog
      v-model="editVisible"
      width="70%"
      top="5vh"
      title="历史数据"
      :close-on-click-modal="false"
      @close="handleclose"
    >
      <el-form>
        <el-form-item>
          <div class="container">
            <div class="handle-box">
              <div>
                <span class="title_name">碳配额量：</span
                ><el-input
                  v-model="seachfrom.carbonQuota"
                  placeholder="请输入"
                  class="handle-input mr10"
                ></el-input>
              </div>
              <div>
                <span class="title_name">碳排量：</span
                ><el-input
                  placeholder="请输入"
                  class="handle-input mr10"
                  v-model="seachfrom.carbonDisplacement"
                ></el-input>
              </div>
              <div>
                <span class="title_name">年份：</span
                ><el-input
                  placeholder="请输入"
                  class="handle-input mr10"
                  v-model="seachfrom.year"
                ></el-input>
              </div>
              <el-button
                type="primary"
                class="icon-search"
                icon="el-icon-search"
                @click="handleSearch"
                >查询</el-button
              >
              <el-button class="icon-search" @click="emptySearch"
                >清空</el-button
              >
            </div>

            <el-table
              :data="tableData"
              border
              class="table"
              style="width: 90%"
              ref="multipleTable"
              header-cell-class-name="table-header"
            >
              <!-- <el-table-column type="selection" label="全选" width="55">
        </el-table-column> -->
              <el-table-column
                prop="id"
                label="序列号"
                width="65"
                align="center"
              >
                <template #default="scope">
                  {{
                    Number(
                      seachfrom.pageSize * (seachfrom.pageNum - 1) +
                        1 +
                        scope.$index
                    )
                  }}
                </template></el-table-column
              >
              <el-table-column
                prop="business"
                label="企业姓名"
              ></el-table-column>
              <el-table-column prop="year" label="年份"></el-table-column>
              <el-table-column
                prop="carbonQuota"
                label="碳配额量"
              ></el-table-column>
              <el-table-column
                prop="carbonDisplacement"
                label="实际碳排量 （吨）"
              ></el-table-column>
              <el-table-column
                prop="carbonPayOff"
                label="应清缴量（吨）"
              ></el-table-column>
              <el-table-column
                prop="carbonAlreadyPayOff"
                label="实际清缴（吨）"
              ></el-table-column>
              <el-table-column prop="createTime" label="清缴记录">
                <template #default="scope">
                  <el-button type="text" @click="handledetail(scope.row)"
                    >查看
                  </el-button>
                </template></el-table-column
              >
            </el-table>
            <!-- <div class="pagination">
              <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page="seachfrom.pageNum"
                :page-size="seachfrom.pageSize"
                :total="pageTotal"
                @current-change="handlePageChange"
              ></el-pagination>
            </div> -->
            <div class="btn_b">
              <el-button type="primary" class="icon-search" @click="handleclose"
                >确定</el-button
              >
            </div>
          </div>
        </el-form-item>
      </el-form>
      <template #footer> </template>
    </el-dialog>
    <view-Data
      :viewVisible="viewVisible"
      v-if="viewVisible"
      :PayOffList="PayOffList"
      :payfooyear="payfooyear"
      @handlecloseView="handlecloseView"
    ></view-Data>
  </div>
</template>
<script>
import { ref, reactive } from "vue";
import api from "../api/api";
import viewData from "../components/view_Data.vue";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  components: { viewData },
  setup(props, context) {
    console.log(context);
    // 参考图片
    const editVisible = ref(context.attrs.editVisible);
    const viewVisible = ref(false);
    const radioValue = ref(context.attrs.radioValue);
    const handleclose = () => {
      editVisible.value = false;
      context.emit("handleclose", editVisible.value);
    };
    const PayOffList = ref([]);
    const payfooyear = ref("");
    const handledetail = (row) => {
      payfooyear.value = row.year;
      api
        .postFormAPI("trade/client/business/carbon/detail/getList", {
          pageNum: 1,
          pageSize: 10,
          businessCarbonId: row.id,
        })
        .then((res) => {
          if (res.data.code == 1) {
            PayOffList.value = res.data.data;

            viewVisible.value = true;
          } else {
            ElMessage.error(res.data.message);
          }
          //数据处理
        })
        .catch((err) => {
          console.log(err);
        });
    };
    const seachfrom = reactive({
      year: null,
      carbonQuota: null,
      woodlandUsePeople: null,
      business: null,
      carbonDisplacement: null,
      isLink: null,
      pageNum: 1,
      pageSize: 20,
    });
    const tableData = ref([]);
    const pageTotal = ref(0);
    const SelectList = ref([]);
    // 获取表格数据
    const getData = () => {
      api
        .postFormAPI("trade/client/business/carbon/getRecordList", seachfrom)
        .then((res) => {
          if (res.data.code == 1) {
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].checked = false;
            }
            tableData.value = res.data.data;
            pageTotal.value = res.data.data.total || 0;
            console.log(res);
          } else {
            ElMessage.error(res.data.message);
          }
          //数据处理
        })
        .catch((err) => {
          console.log(err);
        });
    };
    getData();
    // 查询操作
    const handleSearch = () => {
      seachfrom.pageNum = 1;
      getData();
    };
    const emptySearch = () => {
      seachfrom.pageNum = 1;
      seachfrom.carbonQuota = null;
      seachfrom.year = null;
      seachfrom.business = null;
      seachfrom.carbonDisplacement = null;
    };
    const handleSelect = (res) => {
      radioValue.value = res;
    };
    const handleSaveEdit = () => {
      context.emit(
        "handleSelect",
        tableData.value[radioValue.value],
        radioValue.value
      );
    };
    const handlecloseView = () => {
      viewVisible.value = false;
      context.emit("handlecloseView", viewVisible.value);
    };
    return {
      payfooyear,
      PayOffList,
      radioValue,
      editVisible,
      viewVisible,
      tableData,
      seachfrom,
      pageTotal,
      SelectList,
      handleclose,
      handleSearch,
      emptySearch,
      handleSelect,
      handleSaveEdit,
      handledetail,
      handlecloseView,
    };
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/common.css";
&:deep(.el-dialog) {
  box-shadow: none;
}
&:deep(.el-dialog__headerbtn .el-dialog__close) {
  color: #909399;
  background: #fff;
  width: 30px;
  height: 30px;
  border-radius: 360px;
  font-size: 30px;
}
&:deep(.el-form-item__content) {
  text-align: center;
}
&:deep(.el-input__inner) {
  width: 150px !important;
}
.btn_b {
  padding: 30px;
}
</style>
